package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.service.impl.UserFollowsServiceImpl;
import com.kykj.kykj.vo.UserFollowsVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/userfollows/")
@CrossOrigin//解决跨域问题
@RestController
public class UserFollowsController {
    @Resource
    UserFollowsServiceImpl userFollowsService;

    /**
     * 获取所有关注记录
     * @return JSON
     */
    @GetMapping("userfollows")
    private ResultEntity getUserFollowsList() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(userFollowsService.getUserFollowsList());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 添加关注记录
     * @param userFollowsVo 关注记录 VO
     * @return JSON
     */
    @PostMapping("userfollows")
    private ResultEntity addUserFollows(@RequestBody UserFollowsVo userFollowsVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (userFollowsService.addUserFollows(userFollowsVo) > 0) {
                System.out.println("当前自增的id是" + userFollowsVo.getId() + "------自增");
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode(1);
                resultEntity.setMsg("添加失败！" + userFollowsVo.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 更新关注记录
     * @param userFollowsVo 关注记录 VO
     * @return JSON
     */
    @PutMapping("userfollows")
    private ResultEntity updateUserFollows(@RequestBody UserFollowsVo userFollowsVo) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (userFollowsService.updateUserFollows(userFollowsVo) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            } else {
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 删除关注记录
     * @param id 关注记录 ID
     * @return JSON
     */
    @DeleteMapping("userfollows/{id}")
    private ResultEntity deleteUserFollows(@PathVariable("id") int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            if (userFollowsService.deleteUserFollows(id) > 0) {
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode(1);
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 根据 followerId 获取关注记录
     * @param followerId 关注者 ID
     * @return JSON
     */
    @GetMapping("getuserfollowsbyfollowerid")
    private ResultEntity getUserFollowsByFollowerId(@RequestParam("followerId") int followerId) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(userFollowsService.getUserFollowsByFollowerId(followerId));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }
}